package com.kedacom.ctsp.authz.oauth2;

import com.kedacom.ctsp.authz.oauth2.provider.*;
import com.kedacom.ctsp.authz.security.provider.AuthzSessionRegistry;
import com.kedacom.ctsp.authz.security.provider.RedisSessionRegistryImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutHandler;

/**
 * 注入
 *
 * @author fenghaiju
 * @com.kedacom.ctsp.authz.oauth2
 * @date 2018/5/16-9:51
 * @cloud-rbac-oauth2
 **/
@Configuration
public class OAuth2WebConfiguration {
    /**
     * 跳转到登陆前拦截的地址
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(SavedRequestAwareAuthenticationSuccessHandler.class)
    public OAuth2AuthenticationSuccessHandler restUrlAuthenticationSuccessHandler() {
        return new OAuth2AuthenticationSuccessHandler();
    }

    /**
     * 退出登录
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(OAuth2AuthenticationLogoutHandler.class)
    public LogoutHandler oAuth2AuthenticationLogoutHandler() {
        return new OAuth2AuthenticationLogoutHandler();
    }


    /**
     * 拦截判断jwt的信息
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(RequestAuthenticationJWTFilter.class)
    public RequestAuthenticationJWTFilter requestAuthenticationJWTFilter() {
        return new RequestAuthenticationJWTFilter();
    }

    @Bean
    public AuthzSessionRegistry authzSessionRegistry() {
        return new RedisSessionRegistryImpl();

    }

    /**
     * 判断是什么请求
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(RequestAuthenticationLoginFilter.class)
    public RequestAuthenticationLoginFilter requestAuthenticationLoginFilter() {
        return new RequestAuthenticationLoginFilter();
    }


}
